package com.selfdriving.activity;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.selfdriving.R;
import com.selfdriving.bean.User;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.SystemSettings;

public class LoginActivity extends BaseActivity
{
	@ViewInject(id = R.id.top_text_name)
	TextView top_text_name;

	@ViewInject(id = R.id.top_text_step)
	TextView top_text_step;

	@ViewInject(id = R.id.tvAgreement, click = "tvAgreement")
	TextView tvAgreement;

	@ViewInject(id = R.id.btn_back, click = "btn_back")
	Button btn_back;

	@ViewInject(id = R.id.btn_ok, click = "btn_ok")
	Button btn_ok;

	private EditText user;
	private EditText password;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		user = (EditText) findViewById(R.id.et_username);
		password = (EditText) findViewById(R.id.et_passwrod);
		top_text_name.setText("登录");
		top_text_step.setVisibility(View.GONE);
	}

	public void tvAgreement(View v)
	{
		Intent intent = new Intent(this, FindPwdTabsActivity.class);
		startActivity(intent);
	}

	public void btn_ok(View v)
	{
		String phone = user.getText().toString();
		String pass = password.getText().toString();
		if (phone.length() < 1)
		{
			showToast("请输入您的手机号码");
			return;
		} else if (phone.length() != 11)
		{
			showToast("请输入11位数手机号码");
			return;
		}
		if (pass.length() < 1)
		{
			showToast("请确认您的密码");
			return;
		}
		showLoadingDialog("正在登录中...");
		loading(phone, pass);
	}

	public void loading(final String phone, final String pwd)
	{
		AjaxParams params = new AjaxParams();
		params.put("f", "13");
		params.put("phone", phone);
		params.put("pwd", pwd);
		params.put("img", "http://117.21.209.117:9000/driving/headimg/user.jpg");
		params.put("lat", "" + mApplication.mLatitude);
		params.put("lng", "" + mApplication.mLongitude);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				// TODO Auto-generated method stub
				super.onFailure(t, strMsg);
				dismissLoadingDialog();
				showToast("登录失败");
			}

			@Override
			public void onSuccess(Object t)
			{
				// TODO Auto-generated method stub
				super.onSuccess(t);
				String json = t.toString();
				if (json.contains("000000"))
				{
					try
					{
						JSONObject object = new JSONObject(json);
						String success = object.getString("result");
						if (success.equals("0"))
						{
							String name = object.getString("name");
							String icon = object.getString("img");
							mApplication.user = new User(icon, phone, name,
									pwd, mApplication.mLatitude,
									mApplication.mLongitude);
							SystemSettings.putString(LoginActivity.this,
									"phone", phone);
							SystemSettings.putString(LoginActivity.this, "pwd",
									pwd);
							Intent intent = new Intent(LoginActivity.this,
									HomePageActivity.class);
							startActivity(intent);
						}
						dismissLoadingDialog();
					} catch (JSONException e)
					{
						e.printStackTrace();
						dismissLoadingDialog();
						showToast("登录失败");
					}

				} else if (json.contains("111111"))
				{
					dismissLoadingDialog();
					showToast("登录失败，用户名、密码不正确！");
				}
			}
		});
	}

	public void btn_back(View v)
	{
		this.finish();
	}
}
